import { defineStore } from 'pinia'

// 存储权限状态建
let sessionStorageKey = 'ifUserAuthority'

export const mainData = defineStore('main', {
  state: () => {
    return {
      webTitle: 'Fi Ker 通用毕设系统',
      ifUserAuthority: false,
      userName: null,
      uid: null,
    }
  },
  getters: {},
  actions: {
    setIfUserAuthority(isAuthenticated) {
      this.ifUserAuthority = isAuthenticated // 直接更新 Pinia store 的状态
      sessionStorage.setItem(sessionStorageKey, String(isAuthenticated)) // 同时更新sessionStorage
    },
    loadUserAuthorityFromStorage() {
      // 新增一个action来从sessionStorage加载状态
      const storedAuthority = sessionStorage.getItem(sessionStorageKey)
      if (storedAuthority !== null) {
        this.ifUserAuthority = storedAuthority === 'true'
      }
    },
  },
  // plugins: true,
  persist: [
    {
      key: 'piniaStore', //存储名称
      storage: sessionStorage, // 存储方式
      paths: ['ifUserAuthority', 'userName'], //指定 state 中哪些数据需要被持久化。[] 表示不持久化任何状态，undefined 或 null 表示持久化整个 state
    },
  ],
})
